<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Exception;
use think\Validate;
use app\admin\model\User as UserModel;
use app\admin\model\ShopWithdraw;
use app\admin\model\ShopPriceLog;
use app\admin\model\TaskImg;
use app\admin\model\ProductViewLog;
use app\admin\model\UserOrder;
use app\admin\model\UserCollect;
use app\admin\model\UserAddress;
use app\admin\model\Love;
use app\admin\model\Community;
use app\admin\model\Visit;
use app\admin\model\AreaWorldwide;
use app\admin\model\Product;
use app\admin\model\ProductMessage;
use app\admin\model\UserRoseLog;
use app\admin\model\ConfigData;
use app\admin\model\ChatMessage;
use app\admin\model\ProductWish;
use app\admin\model\MobileCode;
use app\admin\model\UserReport;
use app\admin\model\GroupChat;
use app\admin\model\GroupChatUser;
use getui\GeTui;
/**
 * 苏拉会员接口
 */
class ShopUser extends Api
{
    protected $noNeedLogin = [];

	public function _initialize(){
		$action = request()->action();
	
		if(!in_array($action,$this->noNeedLogin)){
			$ignore = array('host','accept','content-length','content-type');
			$headers = array();
			if($this->request->request('token')){
                $headers['token'] = $this->request->request('token');
            }else{
                foreach($_SERVER as $key=>$value){
                    if(substr($key, 0, 5)==='HTTP_'){
                        $key = substr($key, 5);
                        $key = str_replace('_', ' ', $key);
                        $key = str_replace(' ', '-', $key);
                        $key = strtolower($key);
                        if(!in_array($key, $ignore)){
                            $headers[$key] = $value;
                        }
                    }
                }
            }
			$token=$headers['token'];
			if(strlen($token)!=32){
				$this->error('请登录');
			}
			$this->user=UserModel::where(array('token'=>$token,'is_shop'=>1))->find();
			if(!$this->user){
				$this->error('请登录');
			}
            if($this->user['status'] != 1){
                $this->error('账号异常');
            }
		}
	}


    //苏拉科技我的邀请
	public function shop_my_invite(){
		$type= $this->request->request('type',1);
        if($type == 1){ //查询我的邀请详情
            $page= $this->request->request('page',1);
            $pagesize= $this->request->request('pagesize',10);
            $where = [
                'invite_user_id'=>$this->user['id'],
                'is_shop'=>1
            ];
            $month_log = UserModel::field('id,nickname,avatar,add_time,giveaway_price')->where($where)->order('add_time desc')->limit(($page-1)*$pagesize,$pagesize)->select();
            foreach($month_log as $a=>&$v){
                $v['add_time']=date("Y-m-d",$v['add_time']);
                if(!$v['giveaway_price']){
                    $v['giveaway_price'] = 0;
                }
            }
        }else{
            $where = [
                'invite_user_id'=>$this->user['id'],
                'is_shop'=>1
            ];
            $ids = UserModel::where($where)->column('id');
            if($this->user['is_agent'] == 2){  //是代理
                $num = ShopPriceLog::where(['user_id'=>['in',$ids],'type'=>2])->sum('price');
            }else{
                $num = 0;
                foreach ($ids as $v) {
                    $num += ShopPriceLog::where(['user_id'=>$v,'type'=>2])->order('id')->value('price');
                }
            }
            $month_log = [
                'sum'=>UserModel::field('id')->where($where)->count(),
                'num'=>$this->user['shop_price'] ? $this->user['shop_price']: 0 ,
                'price'=>UserModel::where($where)->sum('giveaway_price'),
            ];
        }
		
        $this->success('发送成功',$month_log);
	}

    //获取记录
    public function get_withdraw_list(){

        //查询提现用户
        $list = ShopWithdraw::where(['user_id'=>$this->user['id']])->select();
        foreach($list as &$v){
            $v['add_time']=date("Y-m-d",$v['add_time']);
            $v['mobile'] = UserModel::where(['id'=>$v['invite_user_id']])->value('mobile');
        }
        $this->success('操作成功',$list);
    }
    //申请提现
    public function add_withdraw(){
        if($this->user['shop_price'] == 0){
            $this->error('提现金额错误');
        }
        //查询该用户是否提现过
        $info = ShopWithdraw::where(['user_id'=>$this->user['id'],'type'=>2])->find();
        if($info && $this->user['shop_price'] < 100){
            $this->error('提现金额不可低于100元');
        }
        Db::startTrans();
		try {
            

            $arr = [
                'user_id'=>$this->user['id'],
                'price'=>$this->user['shop_price'],
                'orderid'=>date("YmdHis").rand(1000,9999),
                'add_time'=>time(),
                'type'=>2,
                'invite_user_id'=>$this->user['id']
            ];
            //判断提现金额是否大于等于500
            if($this->user['shop_price'] < 500){
                $arr['service_price'] = bcmul($this->user['shop_price'], 0.05, 2);
                $arr['price'] = bcsub($this->user['shop_price'], $arr['service_price'], 2);
            }
            ShopWithdraw::insert($arr);
            UserModel::where(['id'=>$this->user['id']])->update(['shop_price'=>0]);
            Db::commit();
            $this->success('操作成功');
		} catch (Exception $e) {
			Db::rollback();
			$this->error($e->getMessage());
		}
    }

    //日志列表
    public function withdrawList(){
        $page= $this->request->request('page',1);
        $pagesize= $this->request->request('pagesize',10);


        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);
        $month = $this->request->request('month','');

        $shop_price = $this->user['shop_price'];
       
        $start_time=strtotime($month.'-01');
		$end_time=strtotime($month.'-01 +1 month');
		$add_time=array('between',array($start_time,$end_time));
        $thismonth_1=strtotime(date("Y-m-01 00:00:01"));
		$thismonth_2=strtotime('+1 month',$thismonth_1);
        $thismonth=array('between',array($thismonth_1,$thismonth_2));
		$rose['yue']=$shop_price;
		$rose['month_in']=ShopWithdraw::where(array('user_id'=>$this->user['id'],'add_time'=>$thismonth,'price'=>['>',0]))->sum('price');	
		$rose['month_out']=ShopWithdraw::where(array('user_id'=>$this->user['id'],'add_time'=>$thismonth,'price'=>['<',0]))->sum('price');	
		$rose['month_log']=ShopWithdraw::where(array('user_id'=>$this->user['id'],'add_time'=>$add_time))->order('add_time desc')->limit(($page-1)*$pagesize,$pagesize)->select();
		foreach($rose['month_log'] as &$v){
			$v['add_time']=date("Y-m-d H:i:s",$v['add_time']);
            $v['status'] = 1;
            if($v['type'] == 1){
                $v['content'] = '邀请用户';
            }elseif($v['type'] == 2){
                $v['content'] = '提现金额';
                //查询该笔提现状态
                $info = ShopWithdraw::where(['id'=>$v['withdraw_id'],'user_id'=>$this->user['id']])->order('id desc')->find();
                if($info && $info['status'] == 'created'){
                    $v['status'] = '2';
                }elseif($info && $info['status'] == 'rejected'){
                    $v['status'] = '-1';
                }
            }elseif($v['type'] == 3){
                $v['content'] = '拒绝提现返还佣金';
            }elseif($v['type'] == 4){
                $v['content'] = '违规删帖惩罚';
            }elseif($v['type'] == 5){
                $v['content'] = '邀请奖励';
            }
		}
        $this->success('操作成功',$rose);
    }



}